package com.nutstudio.feitoys;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.Fade;
import android.util.Log;
import android.view.View;

import com.nutstudio.feitoys.base.BaseActivity;
import com.nutstudio.feitoys.base.CityAdapter;
import com.nutstudio.feitoys.bean.PlaceBean;
import com.nutstudio.feitoys.utils.OkHttpManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import okhttp3.Call;

public class CityActivity extends BaseActivity {

    @InjectView(R.id.list_rv)
    RecyclerView listRv;
    private CityAdapter cityAdapter;
    private List<PlaceBean> dataList = new ArrayList<>();
    private int mainPos = 0;
    private String shengStr="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setEnterTransition(new Fade().setDuration(500));
        getWindow().setExitTransition(new Fade().setDuration(500));
        setContentView(R.layout.activity_city);
        ButterKnife.inject(this);
        initView();
        mainPos = 0;
        getData("http://m.weather.com.cn/manage/citylist/city3jdata/china.html");
    }

    private void getData(String url) {
        Log.d("run",url);
        OkHttpManager.getAsyn(url, new OkHttpManager.ResultCallback() {
            @Override
            public void onError(Exception e) {

            }

            @Override
            public void onResponse( final String string) {
                dataList.clear();
                        try {
                            JSONObject object = new JSONObject(string);
                            if (mainPos == 0) {
                                for (int i = 1; i < 35; i++) {
                                    PlaceBean bean = new PlaceBean();
                                    bean.setId((10100 + i) + "");
                                    bean.setName(object.getString(bean.getId()));
                                    dataList.add(bean);
                                }
                            } else if (mainPos == 1) {
                                int total = 100;
                                for (int i = 1; i < total; i++) {
                                    if (i < 10) {
                                        PlaceBean bean = new PlaceBean();
                                        bean.setId("0" + i);
                                        bean.setName(object.getString(bean.getId()));
                                        dataList.add(bean);
                                    } else {
                                        PlaceBean bean = new PlaceBean();
                                        bean.setId(i + "");
                                        try {
                                            bean.setName(object.getString(bean.getId()));
                                        } catch (JSONException e) {
                                            total = i - 1;
                                            e.printStackTrace();
                                        }
                                        dataList.add(bean);
                                    }
                                }
                                dataList.remove(dataList.size() - 1);
                            }else if (mainPos==2){
                                int total = 100;
                                for (int i = 1; i < total; i++) {
                                    if (i < 10) {
                                        PlaceBean bean = new PlaceBean();
                                        bean.setId("0" + i);
                                        bean.setName(object.getString(bean.getId()));
                                        dataList.add(bean);
                                    } else {
                                        PlaceBean bean = new PlaceBean();
                                        bean.setId(i + "");
                                        try {
                                            bean.setName(object.getString(bean.getId()));
                                        } catch (JSONException e) {
                                            total = i - 1;
                                            e.printStackTrace();
                                        }
                                        dataList.add(bean);
                                    }
                                }
                                dataList.remove(dataList.size() - 1);
                            }

                            cityAdapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }



            }
        });
    }

    private void initView() {
        cityAdapter = new CityAdapter(this, dataList);
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        listRv.setLayoutManager(manager);
        listRv.setAdapter(cityAdapter);
        cityAdapter.setOnItemClickListener(new CityAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (mainPos == 0) {
                    shengStr=dataList.get(position).getId();
                    mainPos = 1;
                    getData("http://m.weather.com.cn/manage/citylist/city3jdata/provshi/" + dataList.get(position).getId() +
                            ".html");
                }else if (mainPos==1){
                    mainPos = 2;
                    getData("http://m.weather.com.cn/manage/citylist/city3jdata/station/" +shengStr+dataList.get(position).getId()+
                            ".html");
                }

            }
        });
    }
}
